//数组  ---> 对象
const arr = [
    {name: " 张三", age: 20},
    {name: " 李四", age: 22},
    {name: " 王五", age: 21},
];

arr.forEach(function(item,index,array){
    console.log(index);
    console.log(item);
});

console.log();
//查找 age>20的第一个元素
const findFrist = arr.find((item )  => 
    item.age > 20
);
console.log(findFrist);
//查找多个元素
const findAll = arr.filter((item) =>
     item.age > 20
);
console.log(findAll);

//映射: elemA--->elemB
//把所有的年龄都加1   在新的对象上面加
const elem1 = arr.map(function(item){
    const newItem = {...item, age: item.age + 1};
    return newItem;
});




//... Spread 展开运算符
const obj = {name:"张三",age: 20};
const obj1 = {name:"李四" , age : 30};
const cloneObj = {...obj,...obj1}
const cloneObj1 = {...obj};



const Arr = ['red','green','blue'];

const Arr1= ['black'];
const cloneArr =[...Arr,...Arr1];


//排序  ---> 不生成新的数组
const sortArr = arr.sort(function(a,b){
   return a.age - b.age;
});
console.log(sortArr);
console.log(sortArr === arr); //true